package com.cmw.auntapp.ui;

import butterknife.BindView;
import butterknife.OnClick;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cmw.auntapp.R;
import com.cmw.auntapp.adapter.SettleListAdapter;
import com.cmw.auntapp.constant.CommandType;
import com.cmw.auntapp.core.base.vo.DataResult;
import com.cmw.auntapp.core.util.DateUtil;
import com.cmw.auntapp.core.util.FastJsonUtil;
import com.cmw.auntapp.model.OrderModel;
import com.cmw.auntapp.model.SettleModel;
import com.cmw.auntapp.presenter.AmountThansferPresenter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.cmw.auntapp.R2.id.btn_left_arrow;
import static com.cmw.auntapp.R2.id.btn_right_arrow;
import static com.cmw.auntapp.R2.id.day;
import static com.cmw.auntapp.core.util.DateUtil.DATE_FORMAT_CN;

/**
 * 用户中心--> 每日结算
 * @Auth chengmingwei
 * @date 2021-04-20 21:34
 */
public class DaySettleActivity  extends BaseActivity {

    @BindView(R.id.lv_settle_list)
    ListView lvSettleList;

    @BindView(R.id.tv_curr_date)    // 当前选择日期
    TextView tvCurrDate;

    @BindView(R.id.tv_receivable_amount)    // 应收
    TextView tvReceivableAmount;

    @BindView(R.id.tv_received_amount)    // 已收/收入
    TextView tvReceivedAmount;

    @BindView(R.id.tv_uncollected_amount)    // 未收
    TextView tvUncollectedAmount;

    private String startDate = null;


    private List<SettleModel> settleList = new ArrayList<>();

    private SettleListAdapter settleListAdapter;

    private AmountThansferPresenter amountThansferPresenter;

    @Override
    protected int getActivityLayout() {
        return R.layout.activity_day_settle;
    }


    @Override
    void onInit(Bundle savedInstanceState) {
        setTitle(R.string.day_settle_title);
        settleListAdapter = new SettleListAdapter(this, settleList);
        lvSettleList.setAdapter(settleListAdapter);
        amountThansferPresenter = new AmountThansferPresenter(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "onResume...");
        setCurrDate(null);
        loadData();
    }

    @OnClick({btn_left_arrow,btn_right_arrow})
    public void doArrow(View view) {
        Integer days = 0;
        int viewId = view.getId();
        switch (viewId) {
            case btn_left_arrow :{
                days = -1;
                break;
            }case btn_right_arrow :{
                days = 1;
                break;
            }
        }
        setCurrDate(days);
        loadData();
    }

    private void loadData() {
        amountThansferPresenter.getSumAmount(startDate);
        amountThansferPresenter.getList(startDate);
    }

    private void setCurrDate(Integer day) {
        if (null == day) {
            Date today = new Date();
            Date preDate = DateUtil.minusDaysToDate(today, 1);
            startDate = DateUtil.dateFormatToStr(preDate);
        } else {
            Date chooseDate = DateUtil.dateFormat(startDate);
            Date preDate = DateUtil.addDaysToDate(chooseDate, day);
            startDate = DateUtil.dateFormatToStr(preDate);
        }

        String currDate = DateUtil.dateFormatToStr(DATE_FORMAT_CN, DateUtil.dateFormat(startDate));
        tvCurrDate.setText(currDate);
    }

    @Override
    protected boolean doFailure(CommandType commandType, DataResult dataResult) {
        switch (commandType) {
            case LIST:{ //订单详情数据

                break;
            } case GET:{ // 应收/实收合计数据
                break;
            }
        }
        return false;
    }

    @Override
    protected boolean doSuccess(CommandType commandType, DataResult dataResult) {
        switch (commandType) {
            case LIST:{ //结算列表数据
                setSettleList(dataResult);
                break;
            } case GET:{ //应收/实收合计数据
                setSumAmount(dataResult);
                break;
            }
        }
        return true;
    }

    private void setSumAmount(DataResult dataResult) {
        JSONObject data = (JSONObject)dataResult.getDatas();
        String receivableAmount = data.getString("receivableAmount");
        String receivedAmount = data.getString("receivedAmount");
        String uncollectedAmount = data.getString("uncollectedAmount");
        tvReceivableAmount.setText(receivableAmount);
        tvReceivedAmount.setText(receivedAmount);
        tvUncollectedAmount.setText(uncollectedAmount);
    }

    private void setSettleList(DataResult dataResult) {
        JSONObject dataJson = (JSONObject) dataResult.getDatas();
        JSONArray jsonArray = dataJson.getJSONArray("list");
        if (null == jsonArray || jsonArray.isEmpty()) {
            settleList = new ArrayList<>();
        } else {
            settleList = FastJsonUtil.convertJsonToList(jsonArray.toString(), SettleModel.class);
        }
        settleListAdapter.setSettleList(settleList);
        settleListAdapter.notifyDataSetChanged();
    }

}